﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using System.IO;
using System.ComponentModel;

/// <summary>
/// 用户导入
/// </summary>
public partial class qy_54tss_Admin_Shop_userimport : System.Web.UI.Page
{
    ECustomer dal = new ECustomer();
    EAddress addrdal = new EAddress();
    BackgroundWorker bgw;

    public string msg = "";
    public int cityid = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        EAdminInfo user = UserHelp.GetAdmin();
        cityid = user.CityID;

        ValidatorSet validator = new ValidatorSet("Admin");
        validator.SetValidator();
        if (!this.Page.IsPostBack)
        {
            int tid = HjNetHelper.GetQueryInt("tid", 0);
            hidTogoId.Value = tid.ToString();
        }
    }

    /// <summary>
    /// 导入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void in_Click(object sender, EventArgs e)
    {
        bgw = new BackgroundWorker();
        bgw.WorkerSupportsCancellation = true;
        bgw.WorkerReportsProgress = true;

        bgw.DoWork += new DoWorkEventHandler(DoWork);
        //bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);

        bgw.RunWorkerAsync();


    }

    /// <summary>
    /// 耗时操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void DoWork(object sender, DoWorkEventArgs e)
    {

        string suffix = Path.GetExtension(this.fuFoodExcel.PostedFile.FileName).ToLower();
        string filepath = "~/upload/excel/";
        if (!System.IO.Directory.Exists(Server.MapPath(filepath)))
        {
            System.IO.Directory.CreateDirectory(Server.MapPath(filepath));
        }
        string sFile = filepath + System.DateTime.Now.ToString("yyyyMMddHHmmssffff") + suffix;  //上传后文件的新名

        if (suffix == ".xls" || suffix == ".xlsx")
        {

            this.fuFoodExcel.PostedFile.SaveAs(Server.MapPath(sFile));

            int index = 0;
            try
            {
                //解析文件并保存数据到数据库

            
                System.Data.DataTable chooseDt = ExcelHelper.ExcelToDataTable(Server.MapPath(sFile), 0);//选中文件DataTable

                foreach (DataRow item in chooseDt.Rows)
                {
                    ECustomerInfo model = new ECustomerInfo();
                    string tell = item[0].ToString().Trim();
                    if (tell.Length == 0)
                    {
                        tell = item[1].ToString().Trim(); ;
                    }


                    model.EMAIL = "";
                    model.Password = WebUtility.GetMd5("123456");
                    model.RegTime = DateTime.Now;
                    model.Point = 0;
                    model.Name = tell;
                    model.IsActivate = 1;
                    model.ActivateCode = WebUtility.RandStr(200);
                    model.GroupID = 0;
                    model.WebSite = cityid.ToString();
                    model.RID = "0";
                    model.TrueName = item[1].ToString().Trim(); ;
                    model.MSN = "";
                    model.Sex = "";
                    model.Tell = tell;
                    model.Phone = "";
                    model.QQ = "";
                    model.State = "0";

                    int userid = dal.batAdd(model); ;

                    EAddressInfo info = new EAddressInfo();
                    info.Receiver = model.TrueName;
                    info.Address = item[2].ToString().Trim(); ;
                    info.BuildingID = 0;
                    info.Phone = "";
                    info.Mobilephone = model.Tell;
                    info.UserID = userid;
                    info.AddTime = DateTime.Now;
                    info.Pri = 0;
                    info.Lat = "";
                    info.Lng = "";

                    addrdal.Add(info);

                    index++;

                }
            }
            catch (Exception ex)
            {
                msg = "已经导入" + index + "条记录，但出现错，请检查请excel文件";
                HJlog.toLog(ex.ToString());
                return;
            }
            msg = "批量导入成功";

        }
        else
        {
            msg = "文件格式不正确，请上传excel文件";
        }


    }

    /// <summary>
    /// 完成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (msg.Length == 0)
        {
            msg = "执行完成";
        }
        AlertScript.RegScript(this.Page,"alert('"+ msg + "');hideload_super();");
    }
}
